package com.example.servlet;

import com.example.beans.RegisterFormBean;
import com.example.beans.UserBean;
import com.example.dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        RegisterFormBean formBean = new RegisterFormBean();
        formBean.setName(request.getParameter("name"));
        formBean.setPassword(request.getParameter("password"));
        formBean.setEmail(request.getParameter("email"));

        UserBean user = new UserBean();
        user.setName(formBean.getName());
        user.setPassword(formBean.getPassword());
        user.setEmail(formBean.getEmail());

        UserDAO userDAO = new UserDAO();
        userDAO.addUser(user);

        // 注册成功后跳转到登录页并添加提示参数
        response.sendRedirect("login.jsp?success=1");
    }
}
